/*
 *绝对路径，相对路径工具类
 */
package com.pclong.fightlandlordserver.util;

/**
 * @date 2011-10-11, 10:20:18
 * @author long@7road.com
 * @description PathUtil类：对路径操作，如相对转绝对，获取web根目录
 * 
 */
public class PathUtil {

    public String getAasolutePath(String path, boolean isWeb) throws IllegalAccessException {
        path = path.replace("/", "\\");
        if (path.indexOf(":") == -1) {
            if (isWeb) {
                return getWebInfPath() + "classes\\" + StringUtil.Trim(path, "\\");
            } else {
                String FileName = getWebClassesPath();
                FileName = FileName.substring(0, FileName.lastIndexOf("\\"));
                return FileName+"\\"+ StringUtil.Trim(path, "\\");
            }
        } else {
            return path;
        }
    }

    public String getWebClassesPath() {
        String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        return StringUtil.subString(path.replace("/", "\\"), 1);

    }

    public String getWebInfPath() throws IllegalAccessException {
        String path = getWebClassesPath();
        if (path.indexOf("WEB-INF") > 0) {
            path = path.substring(0, path.indexOf("WEB-INF") + 8);
        } else {
            throw new IllegalAccessException("路径获取错误");
        }
        return path;
    }

    public String getWebRoot() throws IllegalAccessException {
        String path = getWebClassesPath();
        if (path.indexOf("WEB-INF") > 0) {
            path = path.substring(0, path.indexOf("WEB-INF/classes"));
        } else {
            throw new IllegalAccessException("路径获取错误");
        }
        return path;
    }
}
